//
//  PomeDetailController.m
//  谈诗说词
//
//  Created by dllo on 15/6/12.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "PomeDetailController.h"
#import "PoemModel.h"

#import "WeiboSDK.h"
#import "AppDelegate.h"
#define kRedirectURI    @"http://www.sina.com"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PomeDetailController ()
@property (nonatomic, assign) BOOL collection;
@property (nonatomic, strong) UIButton *collect;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *authorLabel;
@end

@implementation PomeDetailController
- (void)dealloc
{
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    
}

- (void)createView
{
    NSMutableString *changedPome = [NSMutableString stringWithString:self.model.content];
    NSString *str = nil;
    
    NSArray *array = [changedPome componentsSeparatedByString:@"，"];
    NSString *first = [array firstObject];
    
    if (first.length > 6 || first.length < 5) {
        for (int i = 0; i < changedPome.length; i++) {
            str = [changedPome substringWithRange:NSMakeRange(i, 1)];
            if ([str isEqualToString:@"，"]) {
                [changedPome insertString:@"\n\n" atIndex:i + 1];
            }else if ([str isEqualToString:@"。"]){
                [changedPome insertString:@"\n\n" atIndex:i + 1];
            }else if ([str isEqualToString:@"”"]){
                [changedPome insertString:@"\n\n" atIndex:i + 1];
            }else if ([str isEqualToString:@"？"]){
                [changedPome insertString:@"\n\n" atIndex:i + 1];
            }
        }
    }else{
    
    
    for (int i = 0; i < changedPome.length; i++) {
        str = [changedPome substringWithRange:NSMakeRange(i, 1)];
        if ([str isEqualToString:@"。"]) {
         [changedPome insertString:@"\n\n" atIndex:i + 1];
        }
    }
        
    }
    
//    NSMutableString * str1 = [NSMutableString stringWithString:@"11111111"];
//    [str1 insertString:@"b" atIndex:2];
//    NSLog(@"%@", str1);
    
//self.pome = [self.pome stringByReplacingOccurrencesOfString:@"。" withString:@"    \n\n"];
    
    
    
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [imageView setImage:[UIImage imageNamed:@"详情背景.png"]];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    
    self.collect = [[UIButton alloc]initWithFrame:CGRectMake(0.7 * WIDTH, HEIGHT - 0.25 * HEIGHT, 0.13*WIDTH, 0.109 * WIDTH)];
    [self.collect setImage:[UIImage imageNamed:@"收藏.png"] forState:UIControlStateNormal];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray * collectArray = [defaults objectForKey:@"collectArray"];
    for (NSDictionary *dic in collectArray) {
        NSString *content = [dic objectForKey:@"content"];
        NSString *content1 = self.model.content;
        
        if ([content isEqualToString:content1]) {
            [self.collect setImage:[UIImage imageNamed:@"已收藏.png"] forState:UIControlStateNormal];
            [self.collect addTarget:self action:@selector(collectedAction) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:self.collect];
            self.collection = YES;
        }
    }
    if (self.collection == NO) {
        [self.collect setImage:[UIImage imageNamed:@"收藏.png"] forState:UIControlStateNormal];
        [self.collect addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:self.collect];
        
    }
    
    
    
    
    UIButton *share = [[UIButton alloc]initWithFrame:CGRectMake(0.85 * WIDTH, 0.7 * HEIGHT, 0.13 * WIDTH, 0.109 * WIDTH)];
    [share setImage:[UIImage imageNamed:@"分享.png"] forState:UIControlStateNormal];
    [share addTarget:self action:@selector(shareButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:share];
    
    
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 0.2* HEIGHT)];
    //self.scroll.contentSize = CGSizeMake(0, 1000);
    [imageView addSubview:self.scroll];
    
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0.03 * WIDTH, WIDTH, 0.3 * WIDTH)];
    self.titleLabel.font = [UIFont fontWithName:@"yuweij" size:30];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.text = self.model.name;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textAlignment = 1;
    [self.scroll addSubview:self.titleLabel];
    
    self.authorLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0.25 * WIDTH, WIDTH, 0.13 * WIDTH)];
    NSString *a = [NSString stringWithFormat:@"/%@/%@", self.model.dynasty_name, self.model.poet_name];
    self.authorLabel.font = [UIFont fontWithName:@"X-" size:18];
    self.authorLabel.textAlignment = 1;
    self.authorLabel.text = a;
    self.authorLabel.textColor = [UIColor blackColor];
    [self.scroll addSubview:self.authorLabel];
    
    
     NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"X-" size:22]};
    
    CGRect rect = [changedPome boundingRectWithSize:CGSizeMake( WIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    self.poemLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.authorLabel.frame.size.height + 0.28 * WIDTH, WIDTH, rect.size.height)];
   // [self.poemLabel setBackgroundColor:[UIColor grayColor]];
    self.poemLabel.font = [UIFont fontWithName:@"X-" size:22];
    self.poemLabel.textColor = [UIColor blackColor];
    self.poemLabel.text = changedPome;
    self.poemLabel.textAlignment = 1;
    self.poemLabel.numberOfLines = 0;
    self.scroll.contentSize = CGSizeMake(0, rect.size.height + 0.5 * WIDTH);
    [self.scroll addSubview:self.poemLabel];
}


- (void)shareButtonPressed
{

    
        AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
        
        WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
        authRequest.redirectURI = kRedirectURI;
        authRequest.scope = @"all";
        
        WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
        request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                             @"Other_Info_1": [NSNumber numberWithInt:123],
                             @"Other_Info_2": @[@"obj1", @"obj2"],
                             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
        [WeiboSDK sendRequest:request];
    
    
}

- (WBMessageObject *)messageToShare
{
    WBMessageObject *message = [WBMessageObject message];
    message.text = [NSString stringWithFormat:@"#谈诗说词#\n%@\n%@\n%@",self.titleLabel.text, self.authorLabel.text, self.poemLabel.text];
    
    return message;
}



- (void)collectAction
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * arr = [defaults objectForKey:@"collectArray"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.model.content forKey:@"content"];
    [dic setObject:self.model.name forKey:@"name"];
    [dic setObject:self.model.poet_name forKey:@"poet_name"];
    [dic setObject:self.model.dynasty_name forKey:@"dynasty_name"];
    if (self.collection == NO) {
        NSMutableArray * collectArray = [NSMutableArray array];
        [collectArray addObject:dic];
        [defaults setObject:collectArray forKey:@"collectArray"];
        [defaults synchronize];
        [self.collect setImage:[UIImage imageNamed:@"已收藏.png"] forState:UIControlStateNormal];
    } else {
        NSMutableArray * arrayy = [NSMutableArray arrayWithArray:arr];
        [arrayy addObject:dic];
        [defaults removeObjectForKey:@"collectArray"];
        [defaults setObject:arrayy forKey:@"collectArray"];
        [defaults synchronize];
    }
    
    // 发送消息,刷新收藏列表
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTable" object:nil];
}

- (void)collectedAction
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray * collectArray = [defaults objectForKey:@"collectArray"];
    NSMutableArray * collectArray1 = [NSMutableArray array];
    
    for (NSDictionary *dic in collectArray) {
        NSString * content = [dic objectForKey:@"content"];
        NSString * content1 = self.model.content;
        if (![content isEqual:content1]) {
            [collectArray1 addObject:dic];
        }
    }
    [defaults removeObjectForKey:@"collectArray"];
    [defaults setObject:collectArray1 forKey:@"collectArray"];
    [defaults synchronize];
    
    [self.collect setImage:[UIImage imageNamed:@"收藏.png"] forState:UIControlStateNormal];
    self.collection = NO;
    // 发送消息,刷新收藏列表
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTable" object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
